const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');

Page({
  data: {
    income_type: 0, //收入来源 0:全部 1:门店 2:小店
    currentTime: 0, //日期筛选参数,0累计 1今日 2本周 3本月 4本年
    times: ['累计', '今日', '本周', '本月', '本年'],
    showDateChoose: true, //是否显示日期筛选
    showSaleDetail: true, //是否显示收入明细列表
    inputDateHidden: true, //是否隐藏自定义日期输入框
    endDate: "",
    startDate: "",
    page: 1,
    limit: 20,
    total_page: 0,
		showRegulations: false,
		income_statement:'暂无', //收入说明
		sales: [
		], //明细列表
  },


  onShow: function() {
    var shop_user_id = wx.getStorageSync('accountInfo').shop_user_id
    if (shop_user_id == undefined) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    if (shop_user_id == '0') {
      this.setData({
        income_type: 2
      })
    }

    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    var formatDate = year + '-' + month + '-' + day;
    this.setData({
      endDate: formatDate,
      startDate: "请选择开始日期",
      ofEndDate: formatDate,
      shop_user_id: shop_user_id
    })
    this.refreshData();
  },

  //刷新数据
  refreshData: function() {
    var that = this;
    this.setData({
      page: 1,
      total_page: 0
    })
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function() {
      that.loadShopSaleStatisticsData();
      that.loadShopSaleDetailData();
    }, 1000)
  },

  // 收入统计（门店和小店合并统计）
  loadShopSaleStatisticsData: function() {
    var that = this;
    var params = new Object()
    params.time = that.data.currentTime;
    params.type = that.data.income_type;
    if (that.data.currentTime == 0) {
      params.start_time = that.data.startDate;
      params.end_time = that.data.endDate;
    }
    api.POST({
      url: app.globalData.base_url + 'shop/incomeSIStatistics',
      params: params,
      success: function(res) {
        console.log('incomeSIStatistics');
        console.log(res.data);
        util.hiddenLoading();
        that.setData({
          saleSatistics: res.data.data,
        })
      },
    })
  },

  // 收入明细
  loadShopSaleDetailData: function() {
    var that = this;
    var params = new Object()
    params.page = that.data.page;
    params.limit = that.data.limit;
    params.time = that.data.currentTime;
    params.type = that.data.income_type;
    if (that.data.currentTime == 0) {
      params.start_time = that.data.startDate;
      params.end_time = that.data.endDate;
    }
    api.POST({
      url: app.globalData.base_url + 'shop/incomeSIDetail',
      params: params,
      success: function(res) {
        console.log('incomeSIDetail');
        console.log(res.data);
        util.hiddenLoading();
        wx.stopPullDownRefresh();
        if (res.data.code) {
          util.showError(res.data.message);
          return false;
        }
        var paging = res.data.data.paging;
        that.setData({
          page: paging.page,
          total_page: paging.total_page
        })
        var currentSaleDetail = res.data.data.list;
        if (that.data.page == 1) {
          var saleDetailLists = currentSaleDetail;
        } else {
          var saleDetailLists = that.data.sales.concat(currentSaleDetail);
        }
        if (saleDetailLists.length > 0) {
          that.setData({
            sales: saleDetailLists,
            showSaleDetail: true,
          })
        } else {
          that.setData({
            sales: [],
            showSaleDetail: false
          })
        }
      },
      fail: function(res) {
        util.hiddenLoading();
        wx.stopPullDownRefresh();
      }
    })
  },

  //切换收入来源
  incomeType: function(e) {
    var that = this;
    var type = e.currentTarget.dataset.types;
    if (type != this.data.income_type) {
      that.setData({
        income_type: type
      })
      setTimeout(function() {
        that.refreshData();
      }, 500)
    }
  },

  // 切换日期
  swichNav: function(e) {
    var that = this;
    var currentTime = e.target.dataset.currentTime;
    this.setData({
      currentTime: currentTime,
      showDateChoose: (currentTime == 0) ? true : false
    });
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function() {
      that.refreshData();
    }, 500)

  },

  //自定义日期是否隐藏
  actionDayChoose: function(e) {
    this.setData({
      inputDateHidden: !this.data.inputDateHidden
    })
  },

  //加载更多
  onReachBottom: function() {
    var that = this;
    if (this.data.page < this.data.total_page) {
      this.setData({
        page: ++this.data.page
      })
      setTimeout(function() {
        that.loadShopSaleDetailData();
      }, 500)
    }
  },

  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
  },

  //输入开始日期
  startDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  //输入结束日期
  endDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  //时间筛选
  confirm: function() {
    var that = this;
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (a.test(this.data.startDate)) {
      var start_time = this.data.startDate;
    }

    if (a.test(this.data.endDate)) {
      var end_time = this.data.endDate;
    }
    this.setData({
      page: 1,
      total_page: 0,
      start_time: start_time,
      end_time: end_time,
      inputDateHidden: true
    })
    setTimeout(function() {
      that.refreshData();
    }, 500)
  },

  //自定义日期取消
  cancel: function() {
    this.setData({
      inputDateHidden: true
    })
  },

  //去酒掌柜用户版
  jiuzhanggui: function() {
    wx.navigateToMiniProgram({
      appId: 'wx2f5360b509c07b8a',
      path: 'pages/index/index',
      envVersion: 'release', //develop	开发版 trial	体验版  release	正式版
    })
  },


  //退款明细
  refundDetail: function() {
    var startDate = (this.data.startDate == '请选择开始日期' ? '' : this.data.startDate);
    var params = 'type=' + this.data.income_type + '&time=' + this.data.currentTime + '&start_time=' + startDate + '&end_time=' + this.data.endDate
    wx.navigateTo({
			url: '/center/pages/salesRefundDetail/salesRefundDetail?' + params,
    })
  },

	// 使用规则弹框
	showRegulations: function () {
		var that=this;
		// if (that.data.income_statement=='暂无'){
		// 	api.POST({
		// 		url: app.globalData.base_url + 'system/set',
		// 		params: {},
		// 		success: function (res) {
		// 			if (res.data.code == '') {
		// 				that.setData({
		// 					income_statement: res.data.data.income_statement,
		// 				})
		// 			}
		// 		}
		// 	})
		// }
		that.setData({
			showRegulations: true
		})
	},
	// 使用规则弹框隐藏
	hiddenRegulations: function () {
		this.setData({
			showRegulations: false
		})
	},
})